package com.hqins.app.cloud.ribbon;

import com.hqins.app.cloud.common.ResponseObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @Resource
    ResponseObject responseObject;

    Map configMap = new ConcurrentHashMap();

    @HystrixCommand(fallbackMethod = "hiError")
    public ResponseObject hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi/" + name, ResponseObject.class);
    }

    @Value("${myhost}")
    String myhost;
    public ResponseObject fooService(){
        configMap.put("myHost",myhost);
        responseObject.setContent(configMap);
        return responseObject;
    }

    public ResponseObject hiError(String name) {
        responseObject.setSuccess(false);
        responseObject.setStatus(404);
        responseObject.setMessage("SERVICE-HI Not Found");
        return responseObject;
    }
}
